package com.tianyu.service.mqtt.handler.inbound.next;

import com.alibaba.fastjson2.JSONObject;
import com.tianyu.service.mqtt.common.MqttMessageChannel;
import com.tianyu.service.mqtt.model.enums.MessageTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;


/**
 * @author swyan
 */
@Component
@Slf4j
public class CompanyMessageHandler {

    @ServiceActivator(inputChannel = MqttMessageChannel.ChannelName.INBOUND_COMPANY_MESSAGE)
    protected void messageReceived(Message<String> message) {
        String content = message.getPayload();
        final JSONObject jsonObject = JSONObject.parseObject(content);
        final String messageType = jsonObject.getString("messageType");
        final MessageTypeEnum messageTypeEnum = MessageTypeEnum.find(messageType);
        log.info("MessageType: {}->{}", messageType, messageTypeEnum);
        switch (messageTypeEnum) {
            case C_TASK_START -> {
                final JSONObject object = jsonObject.getJSONObject("message");
                final Integer taskId = object.getInteger("taskId");
                final Integer companyId = object.getInteger("companyId");
                final String boxSn = object.getString("boxSn");
                log.info("COMPANY[{}] TASK[{}] START: {}", companyId, taskId, boxSn);
            }
            case C_LIVE_START -> {
                final JSONObject object = jsonObject.getJSONObject("message");
                final Integer taskId = object.getInteger("taskId");
                final Integer companyId = object.getInteger("companyId");
                final String boxSn = object.getString("boxSn");
                final JSONObject pullUrl = object.getJSONObject("pullUrl");
                final String rtmp = pullUrl.getString("rtmp");
                log.info("COMPANY[{}] TASK[{}] START LIVE: {}", companyId, taskId, rtmp);
            }
            case C_TASK_END -> {
                final JSONObject object = jsonObject.getJSONObject("message");
                final Integer taskId = object.getInteger("taskId");
                final Integer companyId = object.getInteger("companyId");
                final String boxSn = object.getString("boxSn");
                log.info("COMPANY[{}] TASK[{}] END: {}", companyId, taskId, boxSn);
            }
            default -> {

            }
        }
    }
}
